package org.spiderflow.mongodb.executor;

import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;
import org.spiderflow.context.SpiderContext;
import org.spiderflow.executor.ShapeExecutor;
import org.spiderflow.model.Shape;
import org.spiderflow.model.SpiderNode;
import org.spiderflow.mongodb.utils.MongoDBUtils;
import org.springframework.stereotype.Component;

import com.mongodb.DBCollection;
@Component
public class MongoDBExecutor implements ShapeExecutor{

	public static final String MONGODB_HOST = "host";
	
	public static final String MONGODB_POST = "port";
	
	public static final String MONGODB_DATABASE = "database";
	
	public static final String MONGODB_TABLE = "table";
	
	public static final String MONGODB_ADMIN_NAME = "adminName";
	
	public static final String MONGODB_PASSOWRD = "password";
	
	public static final String MONGODB_CONTEXT_KEY = "$mongodb_";
	
	
	@Override
	public void execute(SpiderNode node, SpiderContext context, Map<String, Object> variables) {
		String host = node.getStringJsonValue(MONGODB_HOST);
		int port = NumberUtils.toInt(node.getStringJsonValue(MONGODB_POST),27017);
		String database = node.getStringJsonValue(MONGODB_DATABASE);
		String table = node.getStringJsonValue(MONGODB_TABLE);
		String adminName = node.getStringJsonValue(MONGODB_ADMIN_NAME);
		String password = node.getStringJsonValue(MONGODB_PASSOWRD);
		DBCollection DbCollection = MongoDBUtils.createMongoDBTemplate(host, port,database,table,adminName,password);
		context.put(MONGODB_CONTEXT_KEY + node.getNodeId(), DbCollection);
	}
	
	@Override
	public Shape shape() {
		Shape shape = new Shape();
		shape.setImage("");
		shape.setLabel("mongodb");
		shape.setName("mongodb");
		shape.setTitle("mongodb");
		return shape;
	};
	@Override
	public String supportShape() {
		return "mongodb";
	}
}
